/*
 * Copyright 2011 Jin Kwon <jinahya at gmail.com>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package com.googlecode.flac4j.format.metadata;


import com.googlecode.jinahya.io.BitInput;
import com.googlecode.jinahya.io.BitOutput;

import java.io.IOException;


/**
 *
 * @author Jin Kwon <jinahya at gmail.com>
 */
public class Padding extends AbstractData {


    /** GENERATED. */
    private static final long serialVersionUID = 2691772914480608120L;


    public static Data newInstance(final int length) {

        if (length <= 0) {
            throw new IllegalArgumentException("length(" + length + ") <= 0");
        }

        if (length >> 24 != 0) {
            throw new IllegalArgumentException(
                "length(" + length + ") >> 24 != 0");
        }

        final Padding instance = new Padding();
        instance.setLength(length);

        return instance;
    }


    /**
     * Creates a new instance.
     */
    public Padding() {
        super(BlockType.PADDING);
    }


    @Override
    public int getLengthInBytes() {
        return getLength();
    }


    @Override
    public void read(final BitInput input) throws IOException {

        setLength(getHeader().getDataLength());

        for (int i = 0; i < length; i++) {
            assert input.readUnsignedLong(8) == 0x00;
        }
    }


    @Override
    public void write(final BitOutput output) throws IOException {

        for (int i = 0; i < length; i++) {
            output.writeUnsignedInt(8, 0x00);
        }
    }


    // ------------------------------------------------------------------ length
    public int getLength() {

        return length;
    }


    public void setLength(final int length) {
        if (length <= 0) {
            throw new IllegalArgumentException("length <= 0");
        }

        if (length >> 24 != 0) {
            throw new IllegalArgumentException(
                "length(" + length + ") >> 24 != 0");
        }

        this.length = length;
    }


    // ----------------------------------------------------------------- padding
    /**
     * Returns padding.
     *
     * @return padding.
     */
    public byte[] getPadding() {

        return new byte[getLength()];
    }


    /**
     * Sets padding.
     *
     * @param padding padding.
     */
    public void setPadding(final byte[] padding) {

        if (padding == null) {
            throw new NullPointerException("null padding");
        }

        setLength(padding.length);
    }


    /** length. */
    //@XmlElement(required = true)
    private int length;


}

